/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MaterialPack;



/**
 *
 * @author Van persie
 */
import java.util.Vector;
import javax.microedition.lcdui.*;
import java.lang.String;
import Supporter.*;
import javax.microedition.media.Player;
public class Button extends MaterialObject{
    
    // internal
    protected  Image icon;
    protected Image normalIcon;
    //protected Image checkedIcon;
    protected Image pressedIcon;
    protected String text;
    protected int state;  //  0.Normal  1.Checked  2.Pressed
//    protected boolean  isVisible;
//    
//    protected int xPos;
//    protected int yPos;
//    
//    protected int width;
//    protected int height;
    
    // Sound
    protected Player sound;
    
    
    public Button(){}
    public Button(String normalTexture, String txt, int x,int y, int wid, int hei)
    {
        xPos = x;
        yPos = y;
        width = wid;
        height = hei;
        try{
            //Image temp = 
            ImageSupporter imgSpp = new ImageSupporter();
            normalIcon = ImageSupporter.transparent(imgSpp.getImage(normalTexture));
            
            pressedIcon = ImageSupporter.transparent( ImageSupporter.alphaBlend(normalIcon));
            
            text = txt;            
            isVisible = true;
            state = 0;
            //icon =  ImageSupporter.resizeImage(normalIcon, width, height);
            icon = ImageSupporter.scaleImage(normalIcon, width, height);
            //icon = normalIcon;
            
            
            
            
        }catch(Exception e)
        {
            isVisible = false;
        }
        
    }
//    public Button(Image normalTexture, String txt, int x,int y, int wid, int hei)
//    {
//        xPos = x;
//        yPos = y;
//        width = wid;
//        height = hei;
//        try{
//            //Image temp = 
//            //ImageSupporter imgSpp = new ImageSupporter();
//            normalIcon = ImageSupporter.transparent(normalTexture);
//            
//            pressedIcon = ImageSupporter.transparent( ImageSupporter.alphaBlend(normalIcon));
//            
//            text = txt;            
//            isVisible = true;
//            state = 0;
//            //icon =  ImageSupporter.resizeImage(normalIcon, width, height);
//            icon = ImageSupporter.scaleImage(normalIcon, width, height);
//            //icon = normalIcon;
//            
//            
//            
//            
//        }catch(Exception e)
//        {
//            isVisible = false;
//        }
//        
//    }
    public Button(Image normalTexture, String txt, int x,int y, int wid, int hei)
    {
        xPos = x;
        yPos = y;
        width = wid;
        height = hei;
        try{
            //Image temp = 
            normalIcon = ImageSupporter.transparent(normalTexture);
            
            pressedIcon = ImageSupporter.transparent( ImageSupporter.alphaBlend(normalIcon));
            
            text = txt;            
            isVisible = true;
            state = 0;
            //icon =  ImageSupporter.resizeImage(normalIcon, width, height);
            icon = ImageSupporter.scaleImage(normalIcon, width, height);
            //icon = normalIcon;
            
            
            
            
        }catch(Exception e)
        {
            isVisible = false;
        }
        
    }
    public Button(String normalTexture,String pressTexture, String txt, int x,int y, int wid, int hei)
    {
        xPos = x;
        yPos = y;
        width = wid;
        height = hei;
        try{
            //Image temp = 
            ImageSupporter imgSpp = new ImageSupporter();
            normalIcon = ImageSupporter.transparent(imgSpp.getImage(normalTexture));
            
            pressedIcon = ImageSupporter.transparent(imgSpp.getImage(pressTexture));
            
            text = txt;            
            isVisible = true;
            state = 0;
            //icon =  ImageSupporter.resizeImage(normalIcon, width, height);
            icon = ImageSupporter.scaleImage(normalIcon, width, height);
            //icon = normalIcon;
            
            
            
            
        }catch(Exception e)
        {
            isVisible = false;
        }
        
    }
    public Button(Image normalTexture,Image pressTexture, String txt, int x,int y, int wid, int hei)
    {
        xPos = x;
        yPos = y;
        width = wid;
        height = hei;
        try{
            //Image temp = 
            ImageSupporter imgSpp = new ImageSupporter();
            normalIcon = ImageSupporter.transparent(normalTexture);
            
            pressedIcon = ImageSupporter.transparent(pressTexture);
            
            text = txt;            
            isVisible = true;
            state = 0;
            //icon =  ImageSupporter.resizeImage(normalIcon, width, height);
            icon = ImageSupporter.scaleImage(normalIcon, width, height);
            //icon = normalIcon;
            
            
            
            
        }catch(Exception e)
        {
            isVisible = false;
        }
        
    }
    public boolean isContained(int x, int y)
    {
        if( x > getxPos()-width/2 && x < (getxPos() + getWidth()/2) && y > getyPos() && y < (getyPos() + getHeight()))
            return true;
        return false;
    }
    
    public boolean Press(int x, int y)
    {
        if(isContained(x, y)){
            icon = ImageSupporter.scaleImage(pressedIcon, getWidth(), getHeight());
            state = 2;
            return true;
        }
        return false;
    }
    public boolean Release(int x, int y)
    {
        if(state !=0)
        {
            icon = ImageSupporter.scaleImage(normalIcon, getWidth(), getHeight());
            state = 0;
            if(isContained(x, y))
                return true;
        }
        return false;
    }        

    public void paint(Graphics g)
    {
        if(IsVisible() == true)
        {
            g.drawImage(icon, getxPos() , getyPos() ,   Graphics.TOP | Graphics.HCENTER);           
           
        }
    }
}
